package com.example.service;

import com.example.mapper.WorkExperienceMapper;
import com.example.pojo.dto.WorkExperienceDTO;
import com.example.pojo.entity.WorkExperience;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.Date;
import java.util.List;

/**
 * 本文来演示
 *
 * @author 赵建云
 * @create 2023/10/31
 */
@Service
public class WorkExperienceServiceImpl implements WorkExperienceService{
    @Autowired
    private WorkExperienceMapper workExperienceMapper;
    @Override
    public List<WorkExperience> findWorkExperience(Integer jsId) {
        List<WorkExperience> workExperienceList = workExperienceMapper.findWorkExperienceById(jsId);
        return workExperienceList;
    }

    @Override
    public int saveOrUpdateWorkExperience(WorkExperienceDTO workExperienceDTO) {
        if (workExperienceDTO.getId()==null){
            WorkExperience workExperience = new WorkExperience();
            workExperience.setWorkPerformance(workExperienceDTO.getWorkPerformance());
            workExperience.setCompanyName(workExperienceDTO.getCompanyName());
            workExperience.setDimissionTime(workExperienceDTO.getDimissionTime());
            workExperience.setEntryTime(workExperienceDTO.getEntryTime());
            workExperience.setJsId(workExperienceDTO.getJsId());
            workExperience.setPosition(workExperienceDTO.getPosition());
            workExperience.setJobContent(workExperienceDTO.getJobContent());
            workExperience.setProfession(workExperienceDTO.getProfession());
            workExperience.setSection(workExperienceDTO.getSection());
            Date dimissionTime = workExperienceDTO.getDimissionTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            String dimissionTimeString = sdf.format(dimissionTime);

            Date entryTime = workExperienceDTO.getDimissionTime();
            String entryTimeString = sdf.format(entryTime);
            Integer experience = Integer.valueOf(dimissionTimeString)-Integer.valueOf(entryTimeString);
            workExperience.setExperience(experience);
            int count = workExperienceMapper.saveWorkExperience(workExperience);
            return count;
        }else {
            WorkExperience workExperience = new WorkExperience();
            workExperience.setWorkPerformance(workExperienceDTO.getWorkPerformance());
            workExperience.setCompanyName(workExperienceDTO.getCompanyName());
            workExperience.setDimissionTime(workExperienceDTO.getDimissionTime());
            workExperience.setEntryTime(workExperienceDTO.getEntryTime());
            workExperience.setJsId(workExperienceDTO.getJsId());
            workExperience.setPosition(workExperienceDTO.getPosition());
            workExperience.setJobContent(workExperienceDTO.getJobContent());
            workExperience.setProfession(workExperienceDTO.getProfession());
            workExperience.setSection(workExperienceDTO.getSection());
            workExperience.setId(workExperienceDTO.getId());
            Date dimissionTime = workExperienceDTO.getDimissionTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            String dimissionTimeString = sdf.format(dimissionTime);

            Date entryTime = workExperienceDTO.getDimissionTime();
            String entryTimeString = sdf.format(entryTime);
            Integer experience = Integer.valueOf(dimissionTimeString)-Integer.valueOf(entryTimeString);
            workExperience.setExperience(experience);
            int count = workExperienceMapper.updateWorkExperience(workExperience);
            return count;
        }
    }
}
